package top.kaoshanji.p1ch04a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.*;

/**
 * 代码清单 p1ch04a-05：字符串过滤的示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/14 18:53
 */
public class CharsetDecoderExample01 {

    private static final Logger logger = LogManager.getLogger(CharsetDecoderExample01.class);

    public static void main(String[] args) throws CharacterCodingException {
        logger.info("...代码清单 p1ch04a-05：字符串过滤的示例...");

        String str = "你好，123世界！";

        Charset charset = StandardCharsets.ISO_8859_1;
        CharsetDecoder decoder = charset.newDecoder();
        CharsetEncoder encoder = charset.newEncoder();
        encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

        CharBuffer buffer = CharBuffer.wrap(str);
        ByteBuffer byteBuffer = encoder.encode(buffer);
        CharBuffer result = decoder.decode(byteBuffer);

        System.out.println(result.toString());

    }

}
